package net.lijingye.education.controller;

import net.lijingye.education.entity.*;
import net.lijingye.education.exception.DaoException;
import net.lijingye.education.exception.ObjectNotExistsException;
import net.lijingye.education.response.BaseResponse;
import net.lijingye.education.response.ErrorResponse;
import net.lijingye.education.response.SuccessResponse;
import net.lijingye.education.response.meta.ObjectNotExistsMeta;
import net.lijingye.education.service.UserService;
import net.lijingye.education.service.UtilService;
import net.lijingye.education.util.CommonUtil;
import net.lijingye.education.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * Created by LijingYe on 2016/3/27.
 */
@RestController
public class MyEducationController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private UtilService utilService;

    @RequestMapping(value = "/my/topics", method = RequestMethod.GET)
    public BaseResponse getMyTopics(@RequestHeader("Authorization") String accessToken, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            User user = userService.getUserByAccessToken(accessToken);
            List<Lesson> lessonList = user.getLessonList();
            List<Topic> topicList = new ArrayList<>();
            List<TopicLiteVO> topicLiteVOList = new ArrayList<>();
            lessonList.forEach(lesson -> {
                topicList.addAll(lesson.getTopicList());
            });
            topicList.sort((Topic o1, Topic o2) -> {
                if (o1.getTime().before(o2.getTime())) {
                    return 1;
                }
                if (o1.getTime().after(o2.getTime())) {
                    return -1;
                }
                return 0;
            });
            topicList.forEach(topic -> {
                topicLiteVOList.add(new TopicLiteVO(topic));
            });
            return CommonUtil.splitPage(topicLiteVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }

    @RequestMapping(value = "/my/assignments", method = RequestMethod.GET)
    public BaseResponse getMyAssignments(@RequestHeader("Authorization") String accessToken, @RequestParam(value = "status", required = false) String status, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            User user = userService.getUserByAccessToken(accessToken);
            List<Lesson> lessonList = user.getLessonList();
            List<Assignment> assignmentList = new ArrayList<>();
            List<AssignmentLiteVO> assignmentLiteVOList = new ArrayList<>();
            lessonList.forEach(lesson -> {
                assignmentList.addAll(lesson.getAssignmentList());
            });
            assignmentList.sort((Assignment o1, Assignment o2) -> {
                if (o1.getTime().before(o2.getTime())) {
                    return 1;
                }
                if (o1.getTime().after(o2.getTime())) {
                    return -1;
                }
                return 0;
            });
            boolean isFullList = true;
            if (null != status && status.equals("unFinished")) {
                isFullList = false;
            }
            for (Assignment assignment : assignmentList) {
                StudentAssignment studentAssignment = utilService.getStudentAssignmentById(assignment.getId(), user.getId());
                if (isFullList) {
                    assignmentLiteVOList.add(new AssignmentLiteVO(assignment, studentAssignment));
                } else {
                    Date now = new Date();
                    Date endTime = assignment.getEndTime();
                    if (null == studentAssignment && now.before(endTime) && assignment.isNeedSubmit()) {
                        assignmentLiteVOList.add(new AssignmentLiteVO(assignment, studentAssignment));
                    }
                }
            }
            return CommonUtil.splitPage(assignmentLiteVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }

    @RequestMapping(value = "/my/sources", method = RequestMethod.GET)
    public BaseResponse getMySources(@RequestHeader("Authorization") String accessToken, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            User user = userService.getUserByAccessToken(accessToken);
            List<Lesson> lessonList = user.getLessonList();
            List<Source> sourceList = new ArrayList<>();
            List<SourceLiteVO> sourceLiteVOList = new ArrayList<>();
            lessonList.forEach(lesson -> {
                sourceList.addAll(lesson.getSourceList());
            });
            sourceList.sort((Source o1, Source o2) -> {
                if (o1.getTime().before(o2.getTime())) {
                    return 1;
                }
                if (o1.getTime().after(o2.getTime())) {
                    return -1;
                }
                return 0;
            });
            sourceList.forEach(source -> {
                sourceLiteVOList.add(new SourceLiteVO(source));
            });
            return CommonUtil.splitPage(sourceLiteVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }

    @RequestMapping(value = "/my/ppts", method = RequestMethod.GET)
    public BaseResponse getMyPPTs(@RequestHeader("Authorization") String accessToken, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            User user = userService.getUserByAccessToken(accessToken);
            List<Lesson> lessonList = user.getLessonList();
            List<PPT> pptList = new ArrayList<>();
            List<PPTLiteVO> pptLiteVOList = new ArrayList<>();
            lessonList.forEach(lesson -> {
                pptList.addAll(lesson.getPPTList());
            });
            pptList.sort((PPT o1, PPT o2) -> {
                if (o1.getTime().before(o2.getTime())) {
                    return 1;
                }
                if (o1.getTime().after(o2.getTime())) {
                    return -1;
                }
                return 0;
            });
            pptList.forEach(ppt -> {
                pptLiteVOList.add(new PPTLiteVO(ppt));
            });
            return CommonUtil.splitPage(pptLiteVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }
}
